package com.xcity.game.currency.impl;

import com.xcity.game.common.LogCause;
import com.xcity.game.currency.CurrencyType;
import com.xcity.game.currency.IntCurrency;
import com.xcity.game.io.DeserializeStream;
import com.xcity.game.io.SerializeStream;
import com.xcity.pomelo.push.SyncPush;
import com.xcity.pomelo.push.sync.IntSync;

public class Strength extends IntCurrency {

	protected int value2; // 付费体力
	
	public Strength(DeserializeStream stream) {
		super(CurrencyType.STRENGTH,
				SyncPush.SYNC_STRENGTH,
				"[ADDSTR]ID[{}]OLD[{}]NEW[{}]CAUSE[{}]",
				"[DECSTR]ID[{}]OLD[{}]NEW[{}]CAUSE[{}]",
				stream);
		this.value2 = stream.readInt();
	}
	
	public boolean addValue2(int val, String cause) {
		int oldVal = getIntValue();
		if (val <= 0 || oldVal + val <= 0) {
			return false;
		}
		this.value2 += val;
		owner.send(new IntSync(getSyncId(), getIntValue()));
		LOG.info("[ADDSTR2]ID[{}]OLD[{}]NEW[{}]CAUSE[{}]", owner.getId(), oldVal, getIntValue(), cause);
		return true;
	}
	
	@Override
	protected boolean doDec(int val, String causeStr, LogCause causeObj) {
		int oldVal = getIntValue();
		if (val <= 0 || val > oldVal) {
			return false;
		}
		this.value -= val;
		if (this.value < 0) { // 免费体力不足，则扣购买的体力
			val = -this.value;
			this.value = 0;
			this.value2 -= val;
		}
		owner.send(new IntSync(SyncPush.SYNC_STRENGTH, getIntValue()));
		LOG.info("[DECSTR]ID[{}]OLD[{}]NEW[{}]CAUSE[{}]", owner.getId(), oldVal, getIntValue(), causeStr);
		return true;
	}

	@Override
	public int getIntValue() {
		return value + value2;
	}

	@Override
	public void write(SerializeStream out) {
		out.writeInt(this.value);
		out.writeInt(this.value2);
	}
	
}
